package com.zz.ecommerce.declareManage.importB2CDeclareDirect.exception;

import com.zz.ecommerce.common.GridRequest;
import com.zz.ecommerce.common.GridResult;
import com.zz.ecommerce.common.bean.qdc.db.TDeclformHeadQdc;
import com.zz.ecommerce.common.constants.CommonConstants;
import com.zz.ecommerce.common.constants.TableConstants;
import com.zz.ecommerce.declareManage.common.dao.TDeclformHeadQdcDao;
import com.zz.ecommerce.utils.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * Created by fanshilei on 2016/3/28.
 * <p/>
 * 进口B2C申报服务（直购）--异常清单管理
 */
@Controller
@RequestMapping("/importB2CDirect/exception")
@Scope("prototype")
public class DirectExceptionController {

    @Autowired
    private TDeclformHeadQdcDao tDeclformHeadQdcDao;

    @Autowired
    private DirectExceptionService directExceptionService;

    /**
     * 清单异常处理
     *
     * @return
     */
    @RequestMapping(value = "declareFormException", method = {RequestMethod.GET})
    public String declareFormException() {
        return "declareManage/importB2CDeclareDirect/exception/declaration_form_exception";
    }

    /**
     * 查询申报清单管理列表
     *
     * @param tDeclformHeadQdc
     * @param request
     * @return
     */
    @RequestMapping("/queryDeclareList")
    @ResponseBody
    public GridResult queryDeclareList(TDeclformHeadQdc tDeclformHeadQdc, GridRequest request, HttpSession session) {
        tDeclformHeadQdc.setIeFlag(CommonConstants.I_FALG);
        tDeclformHeadQdc.setTradeMode(CommonConstants.B2C_DIRECT);
        tDeclformHeadQdc.setOrgId(SessionUtil.getOrganizationId(session));
        //回执异常清单
        tDeclformHeadQdc.setStatus("1");
        return new GridResult(tDeclformHeadQdcDao.page(TableConstants.T_DECLFORM_HEAD_QDC, tDeclformHeadQdc, request.getStart(), request.getEnd(), request.getSortOrder()), request, tDeclformHeadQdcDao.count(TableConstants.T_DECLFORM_HEAD_QDC, tDeclformHeadQdc));
    }

    /**
     * 记录清单任务表
     *
     * @param id
     * @return
     */
    @RequestMapping("/declare")
    @ResponseBody
    public boolean declare(String id) {
        String[] idArr = id.split(",");
        boolean result = true;
        for (int i = 0; i < idArr.length; i++) {
            if (!directExceptionService.declare(idArr[i])) {
                result = false;
            }
        }
        return result;
    }

}
